package com.jml.生产者消费者.葛一鸣书;


import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author:jml
 * @date: 2019/6/19 0019 22:28
 * @description：
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        //建立缓冲区
        BlockingQueue<PCData> queue = new LinkedBlockingQueue<PCData>(10);

        //建立生产者
        Producer_Book producer1 = new Producer_Book(queue);
        Producer_Book producer2 = new Producer_Book(queue);
        Producer_Book producer3 = new Producer_Book(queue);


        //建立消费者
        Consumer_Book consumer1 = new Consumer_Book(queue);
        Consumer_Book consumer2 = new Consumer_Book(queue);
        Consumer_Book consumer3 = new Consumer_Book(queue);

        //建立线程池
        ExecutorService service = Executors.newCachedThreadPool();
        //运行生产者
        service.execute(producer1);
        service.execute(producer2);
        service.execute(producer3);
        //运行消费者
        service.execute(consumer1);
        service.execute(consumer2);
        service.execute(consumer3);
        Thread.sleep(10 * 1000);

        //停止生产者
        producer1.stop();
        producer2.stop();
        producer3.stop();
        Thread.sleep(3000);
        service.shutdown();
    }
}
